#!/usr/bin/env perl
use warnings;
use strict;

use lib 'lib';

use Test::More tests => 11;
use_ok( 'Guile::Space' );

my $scope = {
    hash => {
        foo     => 23,
        bar     => 2,
        baz     => 'foo',
        fnord   => { foo => 13 },
    },
};
my @tests = (
    {   title   => 'simple hash access',
        code    => q/ (hash 'get 'foo) /,
        result  => 23,
    },
    {   title   => 'hash value usage',
        code    => q/ (* (hash 'get 'foo) (hash 'get 'bar)) /,
        result  => 46,
    },
    {   title   => 'hash access through string',
        code    => q/ (hash 'get "foo") /,
        result  => 23,
    },
    {   title   => 'has access through hash value',
        code    => q/ (hash 'get (hash 'get 'baz)) /,
        result  => 23,
    },
    {   title   => 'hash as hash value',
        code    => q/ ((hash 'get 'fnord) 'get 'foo) /,
        result  => 13,
    },
    {   title   => 'hash keys',
        code    => q/ (hash 'keys) /,
        result  => qr/(?:foo|bar|baz|fnord)/,
        test    => 'like',
    },
    {   title   => 'setting a hash value',
        code    => q/ (begin (hash 'set 'inside 333) (hash 'get 'inside)) /,
        result  => 333,
    },
    {   title   => 'retrieving multiple values',
        code    => q/ ((hash 'values-of 'foo 'bar) 'get 1) /,
        result  => 2,
    },
    {   title   => 'creating a new hash',
        code    => q/ (((make-perl-hash) 'set 'foo 23) 'get 'foo) /,
        result  => 23,
    },
    {   title   => 'transform perl-hash to guile-hash',
        code    => q/ (hashq-ref (hash 'to-hash) 'foo) /,
        result  => 23,
    },
);

my $space = Guile::Space->new;

for my $t (@tests) {

  SKIP: {
        if ( my $msg = $t->{skip} ) { skip $msg => 1 }
        my $test = __PACKAGE__->can( $t->{test} || 'is' );
        $test->( $space->run( code => $t->{code}, scope => $scope ),
                 $t->{result},
                 $t->{title},
        );
    }
}



